<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Class::Struct - declare struct-like datatypes as Perl classes - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/Class::Struct">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./Class::Struct.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/Class::Struct">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/Class::Struct">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/Class::Struct">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/Class::Struct">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/Class::Struct">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/Class::Struct">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/Class::Struct">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/Class::Struct">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/Class::Struct">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/Class::Struct">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/Class::Struct">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/Class::Struct">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/Class::Struct">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/Class::Struct">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/Class::Struct">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/Class::Struct">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/Class::Struct">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/Class::Struct">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/Class::Struct">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/Class::Struct">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/Class::Struct">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/Class::Struct">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/Class::Struct">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/Class::Struct">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/Class::Struct">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/Class::Struct">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/Class::Struct">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/Class::Struct">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/Class::Struct">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/Class::Struct">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/Class::Struct">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/Class::Struct">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/Class::Struct">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/Class::Struct">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/Class::Struct">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/Class::Struct">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/Class::Struct">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/Class::Struct">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/Class::Struct">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/Class::Struct">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/Class::Struct">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/Class::Struct">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/Class::Struct">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/Class::Struct">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/Class::Struct">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/Class::Struct">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/Class::Struct">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/Class::Struct">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/Class::Struct">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/Class::Struct">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/Class::Struct">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/Class::Struct">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/Class::Struct">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/Class::Struct">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/Class::Struct">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/Class::Struct">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/Class::Struct">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/Class::Struct">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/Class::Struct">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/Class::Struct">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/Class::Struct">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/Class::Struct">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/Class::Struct">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/Class::Struct">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/Class::Struct">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/Class::Struct">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/Class::Struct">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/Class::Struct">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/Class::Struct">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/Class::Struct">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/Class::Struct">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/Class::Struct">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/Class::Struct">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/Class::Struct">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/Class::Struct">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/Class::Struct">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/Class::Struct">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/Class::Struct">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/Class::Struct">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/Class::Struct">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./Class::Struct.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/Class::Struct">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/Class::Struct">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/Class::Struct">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/Class::Struct">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/Class::Struct">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/Class::Struct">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/Class::Struct">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/Class::Struct">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/Class::Struct">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/Class::Struct">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/Class::Struct">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/Class::Struct">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/Class::Struct">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/Class::Struct">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/Class::Struct">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/Class::Struct">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/Class::Struct">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/Class::Struct">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/Class::Struct">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/Class::Struct">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/Class::Struct">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/Class::Struct">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/Class::Struct">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/Class::Struct">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/Class::Struct">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/Class::Struct">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/Class::Struct">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/Class::Struct">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/Class::Struct">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/Class::Struct">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/Class::Struct">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/Class::Struct">5.35.1</a>
          <a class="dropdown-item active" href="./Class::Struct.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./Class::Struct.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="./Class::Struct.html">Class::Struct</a>
          <div id="more">
            (<a href="./Class::Struct.txt">source</a>,
            <a href="https://metacpan.org/pod/Class::Struct">CPAN</a>)
          </div>
            <div id="moduleversion">version 0.66</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#The-struct()-function">The struct() function</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#Class-Creation-at-Compile-Time">Class Creation at Compile Time</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#Element-Types-and-Accessor-Methods">Element Types and Accessor Methods</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#Initializing-with-new">Initializing with new</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#EXAMPLES">EXAMPLES</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./Class::Struct.html#Author-and-Modification-History">Author and Modification History</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="./Class::Struct.html#NAME">#</a>NAME</h1>

<p>Class::Struct - declare struct-like datatypes as Perl classes</p>

<h1 id="SYNOPSIS"><a class="permalink" href="./Class::Struct.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use Class::Struct;
        # declare struct, based on array:
struct( CLASS_NAME =&gt; [ ELEMENT_NAME =&gt; ELEMENT_TYPE, ... ]);
        # declare struct, based on hash:
struct( CLASS_NAME =&gt; { ELEMENT_NAME =&gt; ELEMENT_TYPE, ... });

package CLASS_NAME;
use Class::Struct;
        # declare struct, based on array, implicit class name:
struct( ELEMENT_NAME =&gt; ELEMENT_TYPE, ... );

# Declare struct at compile time
use Class::Struct CLASS_NAME =&gt; [ELEMENT_NAME =&gt; ELEMENT_TYPE, ...];
use Class::Struct CLASS_NAME =&gt; {ELEMENT_NAME =&gt; ELEMENT_TYPE, ...};

# declare struct at compile time, based on array, implicit
# class name:
package CLASS_NAME;
use Class::Struct ELEMENT_NAME =&gt; ELEMENT_TYPE, ... ;

package Myobj;
use Class::Struct;
        # declare struct with four types of elements:
struct( s =&gt; &#39;$&#39;, a =&gt; &#39;@&#39;, h =&gt; &#39;%&#39;, c =&gt; &#39;My_Other_Class&#39; );

$obj = new Myobj;               # constructor

                                # scalar type accessor:
$element_value = $obj-&gt;s;           # element value
$obj-&gt;s(&#39;new value&#39;);               # assign to element

                                # array type accessor:
$ary_ref = $obj-&gt;a;                 # reference to whole array
$ary_element_value = $obj-&gt;a(2);    # array element value
$obj-&gt;a(2, &#39;new value&#39;);            # assign to array element

                                # hash type accessor:
$hash_ref = $obj-&gt;h;                # reference to whole hash
$hash_element_value = $obj-&gt;h(&#39;x&#39;); # hash element value
$obj-&gt;h(&#39;x&#39;, &#39;new value&#39;);          # assign to hash element

                                # class type accessor:
$element_value = $obj-&gt;c;           # object reference
$obj-&gt;c-&gt;method(...);               # call method of object
$obj-&gt;c(new My_Other_Class);        # assign a new object</code></pre>

<h1 id="DESCRIPTION"><a class="permalink" href="./Class::Struct.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p><code>Class::Struct</code> exports a single function, <code>struct</code>. Given a list of element names and types, and optionally a class name, <code>struct</code> creates a Perl 5 class that implements a &quot;struct-like&quot; data structure.</p>

<p>The new class is given a constructor method, <code>new</code>, for creating struct objects.</p>

<p>Each element in the struct data has an accessor method, which is used to assign to the element and to fetch its value. The default accessor can be overridden by declaring a <code>sub</code> of the same name in the package. (See Example 2.)</p>

<p>Each element&#39;s type can be scalar, array, hash, or class.</p>

<h2 id="The-struct()-function"><a class="permalink" href="./Class::Struct.html#The-struct()-function">#</a><a id="The"></a><a id="The-struct-function"></a>The <code>struct()</code> function</h2>

<p>The <code>struct</code> function has three forms of parameter-list.</p>

<pre><code>struct( CLASS_NAME =&gt; [ ELEMENT_LIST ]);
struct( CLASS_NAME =&gt; { ELEMENT_LIST });
struct( ELEMENT_LIST );</code></pre>

<p>The first and second forms explicitly identify the name of the class being created. The third form assumes the current package name as the class name.</p>

<p>An object of a class created by the first and third forms is based on an array, whereas an object of a class created by the second form is based on a hash. The array-based forms will be somewhat faster and smaller; the hash-based forms are more flexible.</p>

<p>The class created by <code>struct</code> must not be a subclass of another class other than <code>UNIVERSAL</code>.</p>

<p>It can, however, be used as a superclass for other classes. To facilitate this, the generated constructor method uses a two-argument blessing. Furthermore, if the class is hash-based, the key of each element is prefixed with the class name (see <i>Perl Cookbook</i>, Recipe 13.12).</p>

<p>A function named <code>new</code> must not be explicitly defined in a class created by <code>struct</code>.</p>

<p>The <i>ELEMENT_LIST</i> has the form</p>

<pre><code class="plaintext">NAME =&gt; TYPE, ...</code></pre>

<p>Each name-type pair declares one element of the struct. Each element name will be defined as an accessor method unless a method by that name is explicitly defined; in the latter case, a warning is issued if the warning flag (<b>-w</b>) is set.</p>

<h2 id="Class-Creation-at-Compile-Time"><a class="permalink" href="./Class::Struct.html#Class-Creation-at-Compile-Time">#</a><a id="Class"></a>Class Creation at Compile Time</h2>

<p><code>Class::Struct</code> can create your class at compile time. The main reason for doing this is obvious, so your class acts like every other class in Perl. Creating your class at compile time will make the order of events similar to using any other class ( or Perl module ).</p>

<p>There is no significant speed gain between compile time and run time class creation, there is just a new, more standard order of events.</p>

<h2 id="Element-Types-and-Accessor-Methods"><a class="permalink" href="./Class::Struct.html#Element-Types-and-Accessor-Methods">#</a><a id="Element"></a>Element Types and Accessor Methods</h2>

<p>The four element types -- scalar, array, hash, and class -- are represented by strings -- <code>&#39;$&#39;</code>, <code>&#39;@&#39;</code>, <code>&#39;%&#39;</code>, and a class name -- optionally preceded by a <code>&#39;*&#39;</code>.</p>

<p>The accessor method provided by <code>struct</code> for an element depends on the declared type of the element.</p>

<dl>

<dt id="Scalar-(&#39;$&#39;-or-&#39;*$&#39;)"><a class="permalink" href="./Class::Struct.html#Scalar-(&#39;$&#39;-or-&#39;*$&#39;)">#</a><a id="Scalar"></a><a id="Scalar-or"></a>Scalar (<code>&#39;$&#39;</code> or <code>&#39;*$&#39;</code>)</dt>
<dd>

<p>The element is a scalar, and by default is initialized to <code>undef</code> (but see <a href="./Class::Struct.html#Initializing-with-new">&quot;Initializing with new&quot;</a>).</p>

<p>The accessor&#39;s argument, if any, is assigned to the element.</p>

<p>If the element type is <code>&#39;$&#39;</code>, the value of the element (after assignment) is returned. If the element type is <code>&#39;*$&#39;</code>, a reference to the element is returned.</p>

</dd>
<dt id="Array-(&#39;@&#39;-or-&#39;*@&#39;)"><a class="permalink" href="./Class::Struct.html#Array-(&#39;@&#39;-or-&#39;*@&#39;)">#</a><a id="Array"></a><a id="Array-or"></a>Array (<code>&#39;@&#39;</code> or <code>&#39;*@&#39;</code>)</dt>
<dd>

<p>The element is an array, initialized by default to <code>()</code>.</p>

<p>With no argument, the accessor returns a reference to the element&#39;s whole array (whether or not the element was specified as <code>&#39;@&#39;</code> or <code>&#39;*@&#39;</code>).</p>

<p>With one or two arguments, the first argument is an index specifying one element of the array; the second argument, if present, is assigned to the array element. If the element type is <code>&#39;@&#39;</code>, the accessor returns the array element value. If the element type is <code>&#39;*@&#39;</code>, a reference to the array element is returned.</p>

<p>As a special case, when the accessor is called with an array reference as the sole argument, this causes an assignment of the whole array element. The object reference is returned.</p>

</dd>
<dt id="Hash-(&#39;%&#39;-or-&#39;*%&#39;)"><a class="permalink" href="./Class::Struct.html#Hash-(&#39;%25&#39;-or-&#39;*%25&#39;)">#</a><a id="Hash"></a><a id="Hash-or"></a>Hash (<code>&#39;%&#39;</code> or <code>&#39;*%&#39;</code>)</dt>
<dd>

<p>The element is a hash, initialized by default to <code>()</code>.</p>

<p>With no argument, the accessor returns a reference to the element&#39;s whole hash (whether or not the element was specified as <code>&#39;%&#39;</code> or <code>&#39;*%&#39;</code>).</p>

<p>With one or two arguments, the first argument is a key specifying one element of the hash; the second argument, if present, is assigned to the hash element. If the element type is <code>&#39;%&#39;</code>, the accessor returns the hash element value. If the element type is <code>&#39;*%&#39;</code>, a reference to the hash element is returned.</p>

<p>As a special case, when the accessor is called with a hash reference as the sole argument, this causes an assignment of the whole hash element. The object reference is returned.</p>

</dd>
<dt id="Class-(&#39;Class_Name&#39;-or-&#39;*Class_Name&#39;)"><a class="permalink" href="./Class::Struct.html#Class-(&#39;Class_Name&#39;-or-&#39;*Class_Name&#39;)">#</a><a id="Class1"></a><a id="Class-Class_Name-or-Class_Name"></a>Class (<code>&#39;Class_Name&#39;</code> or <code>&#39;*Class_Name&#39;</code>)</dt>
<dd>

<p>The element&#39;s value must be a reference blessed to the named class or to one of its subclasses. The element is not initialized by default.</p>

<p>The accessor&#39;s argument, if any, is assigned to the element. The accessor will <code>croak</code> if this is not an appropriate object reference.</p>

<p>If the element type does not start with a <code>&#39;*&#39;</code>, the accessor returns the element value (after assignment). If the element type starts with a <code>&#39;*&#39;</code>, a reference to the element itself is returned.</p>

</dd>
</dl>

<h2 id="Initializing-with-new"><a class="permalink" href="./Class::Struct.html#Initializing-with-new">#</a><a id="Initializing"></a>Initializing with <code>new</code></h2>

<p><code>struct</code> always creates a constructor called <code>new</code>. That constructor may take a list of initializers for the various elements of the new struct.</p>

<p>Each initializer is a pair of values: <i>element name</i><code> =&gt; </code><i>value</i>. The initializer value for a scalar element is just a scalar value. The initializer for an array element is an array reference. The initializer for a hash is a hash reference.</p>

<p>The initializer for a class element is an object of the corresponding class, or of one of it&#39;s subclasses, or a reference to a hash containing named arguments to be passed to the element&#39;s constructor.</p>

<p>See Example 3 below for an example of initialization.</p>

<h1 id="EXAMPLES"><a class="permalink" href="./Class::Struct.html#EXAMPLES">#</a>EXAMPLES</h1>

<dl>

<dt id="Example-1"><a class="permalink" href="./Class::Struct.html#Example-1">#</a><a id="Example"></a>Example 1</dt>
<dd>

<p>Giving a struct element a class type that is also a struct is how structs are nested. Here, <code>Timeval</code> represents a time (seconds and microseconds), and <code>Rusage</code> has two elements, each of which is of type <code>Timeval</code>.</p>

<pre><code>use Class::Struct;

struct( Rusage =&gt; {
    ru_utime =&gt; &#39;Timeval&#39;,  # user time used
    ru_stime =&gt; &#39;Timeval&#39;,  # system time used
});

struct( Timeval =&gt; [
    tv_secs  =&gt; &#39;$&#39;,        # seconds
    tv_usecs =&gt; &#39;$&#39;,        # microseconds
]);

# create an object:
my $t = Rusage-&gt;new(ru_utime=&gt;Timeval-&gt;new(),
    ru_stime=&gt;Timeval-&gt;new());

# $t-&gt;ru_utime and $t-&gt;ru_stime are objects of type Timeval.
# set $t-&gt;ru_utime to 100.0 sec and $t-&gt;ru_stime to 5.0 sec.
$t-&gt;ru_utime-&gt;tv_secs(100);
$t-&gt;ru_utime-&gt;tv_usecs(0);
$t-&gt;ru_stime-&gt;tv_secs(5);
$t-&gt;ru_stime-&gt;tv_usecs(0);</code></pre>

</dd>
<dt id="Example-2"><a class="permalink" href="./Class::Struct.html#Example-2">#</a><a id="Example1"></a>Example 2</dt>
<dd>

<p>An accessor function can be redefined in order to provide additional checking of values, etc. Here, we want the <code>count</code> element always to be nonnegative, so we redefine the <code>count</code> accessor accordingly.</p>

<pre><code>package MyObj;
use Class::Struct;

# declare the struct
struct ( &#39;MyObj&#39;, { count =&gt; &#39;$&#39;, stuff =&gt; &#39;%&#39; } );

# override the default accessor method for &#39;count&#39;
sub count {
    my $self = shift;
    if ( @_ ) {
        die &#39;count must be nonnegative&#39; if $_[0] &lt; 0;
        $self-&gt;{&#39;MyObj::count&#39;} = shift;
        warn &quot;Too many args to count&quot; if @_;
    }
    return $self-&gt;{&#39;MyObj::count&#39;};
}

package main;
$x = new MyObj;
print &quot;\$x-&gt;count(5) = &quot;, $x-&gt;count(5), &quot;\n&quot;;
                        # prints &#39;$x-&gt;count(5) = 5&#39;

print &quot;\$x-&gt;count = &quot;, $x-&gt;count, &quot;\n&quot;;
                        # prints &#39;$x-&gt;count = 5&#39;

print &quot;\$x-&gt;count(-5) = &quot;, $x-&gt;count(-5), &quot;\n&quot;;
                        # dies due to negative argument!</code></pre>

</dd>
<dt id="Example-3"><a class="permalink" href="./Class::Struct.html#Example-3">#</a><a id="Example2"></a>Example 3</dt>
<dd>

<p>The constructor of a generated class can be passed a list of <i>element</i>=&gt;<i>value</i> pairs, with which to initialize the struct. If no initializer is specified for a particular element, its default initialization is performed instead. Initializers for non-existent elements are silently ignored.</p>

<p>Note that the initializer for a nested class may be specified as an object of that class, or as a reference to a hash of initializers that are passed on to the nested struct&#39;s constructor.</p>

<pre><code>use Class::Struct;

struct Breed =&gt;
{
    name  =&gt; &#39;$&#39;,
    cross =&gt; &#39;$&#39;,
};

struct Cat =&gt;
[
    name     =&gt; &#39;$&#39;,
    kittens  =&gt; &#39;@&#39;,
    markings =&gt; &#39;%&#39;,
    breed    =&gt; &#39;Breed&#39;,
];


my $cat = Cat-&gt;new( name =&gt; &#39;Socks&#39;,
           kittens  =&gt; [&#39;Monica&#39;, &#39;Kenneth&#39;],
           markings =&gt; { socks=&gt;1, blaze=&gt;&quot;white&quot; },
           breed    =&gt; Breed-&gt;new(name=&gt;&#39;short-hair&#39;, cross=&gt;1),
      or:  breed    =&gt; {name=&gt;&#39;short-hair&#39;, cross=&gt;1},
                  );

print &quot;Once a cat called &quot;, $cat-&gt;name, &quot;\n&quot;;
print &quot;(which was a &quot;, $cat-&gt;breed-&gt;name, &quot;)\n&quot;;
print &quot;had 2 kittens: &quot;, join(&#39; and &#39;, @{$cat-&gt;kittens}), &quot;\n&quot;;</code></pre>

</dd>
</dl>

<h1 id="Author-and-Modification-History"><a class="permalink" href="./Class::Struct.html#Author-and-Modification-History">#</a><a id="Author"></a>Author and Modification History</h1>

<p>Modified by Damian Conway, 2001-09-10, v0.62.</p>

<pre><code>Modified implicit construction of nested objects.
Now will also take an object ref instead of requiring a hash ref.
Also default initializes nested object attributes to undef, rather
than calling object constructor without args
Original over-helpfulness was fraught with problems:
    * the class&#39;s constructor might not be called &#39;new&#39;
    * the class might not have a hash-like-arguments constructor
    * the class might not have a no-argument constructor
    * &quot;recursive&quot; data structures didn&#39;t work well:
              package Person;
              struct { mother =&gt; &#39;Person&#39;, father =&gt; &#39;Person&#39;};</code></pre>

<p>Modified by Casey West, 2000-11-08, v0.59.</p>

<pre><code class="plaintext">Added the ability for compile time class creation.</code></pre>

<p>Modified by Damian Conway, 1999-03-05, v0.58.</p>

<pre><code class="plaintext">Added handling of hash-like arg list to class ctor.

Changed to two-argument blessing in ctor to support
derivation from created classes.

Added classname prefixes to keys in hash-based classes
(refer to &quot;Perl Cookbook&quot;, Recipe 13.12 for rationale).

Corrected behaviour of accessors for &#39;*@&#39; and &#39;*%&#39; struct
elements.  Package now implements documented behaviour when
returning a reference to an entire hash or array element.
Previously these were returned as a reference to a reference
to the element.</code></pre>

<p>Renamed to <code>Class::Struct</code> and modified by Jim Miner, 1997-04-02.</p>

<pre><code class="plaintext">members() function removed.
Documentation corrected and extended.
Use of struct() in a subclass prohibited.
User definition of accessor allowed.
Treatment of &#39;*&#39; in element types corrected.
Treatment of classes as element types corrected.
Class name to struct() made optional.
Diagnostic checks added.</code></pre>

<p>Originally <code>Class::Template</code> by Dean Roehrich.</p>

<pre><code># Template.pm   --- struct/member template builder
#   12mar95
#   Dean Roehrich
#
# changes/bugs fixed since 28nov94 version:
#  - podified
# changes/bugs fixed since 21nov94 version:
#  - Fixed examples.
# changes/bugs fixed since 02sep94 version:
#  - Moved to Class::Template.
# changes/bugs fixed since 20feb94 version:
#  - Updated to be a more proper module.
#  - Added &quot;use strict&quot;.
#  - Bug in build_methods, was using @var when @$var needed.
#  - Now using my() rather than local().
#
# Uses perl5 classes to create nested data types.
# This is offered as one implementation of Tom Christiansen&#39;s
# &quot;structs.pl&quot; idea.</code></pre>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
